package com.netx.cubigraf.backups;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.netx.data.DatabaseException;
import com.netx.ebs.EbsRequest;
import com.netx.ebs.EbsResponse;
import com.netx.ebs.EbsServlet;
import com.netx.ebs.TemplateValues;
import com.netx.generics.basic.Context;
import com.netx.generics.tasks.TaskManager;
import com.netx.generics.tasks.BackgroundTask;
import com.netx.generics.util.ByteValue;
import com.netx.generics.util.ByteValue.MEASURE;
import com.netx.generics.util.Strings;


public class SrvInfo extends EbsServlet {

	public SrvInfo() {
		super();
	}

	protected void doGet(EbsRequest request, EbsResponse response) throws IOException, DatabaseException {
		TaskManager tm = (TaskManager)Context.getRootContext().getProperty("task-manager").objectValue();
		
		TemplateValues tv = new TemplateValues();
		tv.put("title", "Info");
		tv.put("num_fg_tasks", tm.getNumRunningForegroundTasks());
		tv.put("num_bg_tasks", tm.getNumRunningBackgroundTasks());
		
		// Show details about the background tasks:
		List<TemplateValues> taskDetails = new ArrayList<TemplateValues>();
		for(BackgroundTask t : tm.getBackgroundTasks()) {
			DirectoryCopyTask dTask = (DirectoryCopyTask)t;
			TemplateValues tvDetails = new TemplateValues();
			tvDetails.put("user", dTask.getUsername());
			tvDetails.put("ref", dTask.getReference());
			double bytesCopied = ByteValue.convert(dTask.getBytesCopied(), MEASURE.BYTES, MEASURE.MEGABYTES);
			double bytesTotal = ByteValue.convert(dTask.getBytesTotal(), MEASURE.BYTES, MEASURE.MEGABYTES);
			tvDetails.put("copied", Strings.valueOf(bytesCopied, 0, 2));
			tvDetails.put("total", Strings.valueOf(bytesTotal, 0, 2));
			taskDetails.add(tvDetails);
		}
		tv.putList("tasks", taskDetails);
		
		response.setContentType("text/html");
		response.sendDisableCache();
		response.sendTemplate("page.info.tplt", tv);
	}

}
